<?php

namespace Youni\Controller;

use Think\Controller;
use Think\Upload;

class ResoController extends YnController
{
    public function index()
    {
        $mod = M('reso');
        $page = new \Think\Page($mod->count(), 15);
        $list = $mod->order("addtime DESC")->limit($page->firstRow, $page->listRows)->select();
        $list = list_qiniu_cdn_url($list, 'picname');
        $this->assign("mod", $list);
        $this->assign("pageinfo", $page->show());
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $this->display("index");
    }

    public function search(){
        $keyword =$_POST['keyword'];
        $mod = M('reso');
        $page = new \Think\Page($mod->where("title like '%$keyword%'")->count(), 15);
        $list = $mod->where("title like '%$keyword%'")->order("addtime DESC")->limit($page->firstRow, $page->listRows)->select();
        $list = list_qiniu_cdn_url($list, 'picname');
        $this->assign("mod", $list);
        $this->assign("pageinfo", $page->show());
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $this->display("index");
    }

    public function tans()
    {
        $id = $_GET['id'];
        M('reso')->where("id='{$id}'")->save(['status' => 1]);

        $mod = M('reso');
        $page = new \Think\Page($mod->count(), 15);
        $list = $mod->order("addtime DESC")->limit($page->firstRow, $page->listRows)->select();
        $list = list_qiniu_cdn_url($list, 'picname');
        $this->assign("mod", $list);
        $this->assign("pageinfo", $page->show());
        $this->display("index");
    }

    public function guan()
    {
        $id = $_GET['id'];
        M('reso')->where("id='{$id}'")->save(['status' => 2]);
        $mod = M('reso');
        $page = new \Think\Page($mod->count(), 15);
        $list = $mod->order("addtime DESC")->limit($page->firstRow, $page->listRows)->select();
        $list = list_qiniu_cdn_url($list, 'picname');
        $this->assign("mod", $list);
        $this->assign("pageinfo", $page->show());
        $this->display("index");
    }

    public function tans1()
    {
        $id = $_GET['id'];
        M('reso')->where("id='{$id}'")->save(['hot' => 1]);

        $mod = M('reso');
        $page = new \Think\Page($mod->count(), 15);
        $list = $mod->order("addtime DESC")->limit($page->firstRow, $page->listRows)->select();
        $list = list_qiniu_cdn_url($list, 'picname');
        $this->assign("mod", $list);
        $this->assign("pageinfo", $page->show());
        $this->display("index");
    }

    public function guan1()
    {
        $id = $_GET['id'];
        M('reso')->where("id='{$id}'")->save(['hot' => 2]);
        $mod = M('reso');
        $page = new \Think\Page($mod->count(), 15);
        $list = $mod->order("addtime DESC")->limit($page->firstRow, $page->listRows)->select();
	     $list = list_qiniu_cdn_url($list, 'picname');
	    $this->assign("mod", $list);
		$this->assign("pageinfo", $page->show());
		$this->display("index");
	}

	public function editDizhi(){
        $id = $_POST['id'];
        $dizhi = $_POST['dizhi'];
        $r = M('reso')->where("id='{$id}'")->save(['dizhi' => $dizhi]);
        if ($r){
            echo json_encode(['code' => '1', 'msg' => '地址更新成功！'], JSON_UNESCAPED_UNICODE);
        }else{
            echo json_encode(['code' => '0', 'msg' => '地址未变更'], JSON_UNESCAPED_UNICODE);
        }

    }

    public function insert()
    {
        $rands = rand();
        $ka = substr($rands, 0, 4);
        $_POST['zan'] = $ka;
        if (!empty($_FILES)) {
            //如果有文件上传 上传附件
            $this->_upload();
            //$this->forward();
        }
        $_POST['addtime'] = time();
        parent::insert();
    }

    public function edit()
    {
        $id = $_GET['id'];
        $mod = M('reso')->where("id={$id}")->find();
        $mod['picname'] = get_qiniu_cdn_url($mod['picname']);
        $this->assign("mod", $mod);
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $this->display("edit");
    }

    public function add()
    {
        $mod = M("type")->order("id ASC")->select();
        $this->assign("mod", $mod);
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $this->display("add");
    }

    //执行修改
    public function update()
    {
        if (!empty($_FILES) && $_FILES["uploadpic"]["error"] == 0) {
            //如果有文件上传 上传附件
            $this->_upload();
            //$this->forward();
            //删除原图
            $model = D("reso");
            $id = $_REQUEST ["id"];
            $ob = $model->find($id);
            if (!empty($ob)) {
                @unlink('./Public/uploads/' . $ob['picname']);
                //@unlink('./Public/uploads/links/s_'.$ob['logo']);
            }
        }
        parent::update();
    }


    // 文件上传
    protected function _upload()
    {
        // 七牛云配置
        $setting = C('UPLOAD_SITEIMG_QINIU');
        $upload = new \Think\Upload($setting);// 实例化上传类
        $info = $upload->upload();
        if (!$info) {
            $this->error($upload->getError());
        } else {
            //取得成功上传的文件信息
            $uploadList = array_values($info);
            $picname = $uploadList[0]['name'];
            $picname = str_replace('/', '_', $picname);
            $_POST['picname'] = $picname;
        }
    }
}
